import * as echarts from 'echarts'
import type { EChartsOption } from 'echarts';
import { onUnmounted } from "@vue/runtime-core";

export default function useEchart(el) {
  const echartInstance = echarts.init(el, 'null', { renderer: 'svg'})

  onUnmounted(() => {
    echartInstance && echartInstance.dispose()
  })

  function setOption(option: EChartsOption) {
    echartInstance.setOption(option)
  }
  function resizechart() {
    echartInstance.resize()
  }
  window.addEventListener('resize', () => {
    resizechart()
  })

  return {
    echartInstance,
    setOption,
    resizechart
  }
}